<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .big {
            width: 250px;
            height: 250px;
            border: 1px solid #000;
            margin: 0 100px;
        }
        
        img {
            display: block;
            width: 80%;
            margin: 0 auto;
        }
        
        .shit {
            width: 100px;
            height: 100px;
            background-color: grey;
            opacity: 0.4;
            /* position: fixed; */
            display: none;
        }
        
        .bigBox {
            width: 300px;
            height: 300px;
            margin: 0 10px;
            display: none;
            overflow: hidden;
        }
        
        .bigBox img {
            width: 60%;
            transform: scale(2);
        }
        
        .set {
            display: flex;
        }
    </style>
</head>

<body>

    <div class="set">
        <div class="big">
            <img src="./img/cw1.png" alt="">
            <div class="shit"></div>
        </div>
        <div class="bigBox">
            <img src="./img/cw1.png" alt="">
        </div>
    </div>

</body>
<script>
    let mask = document.querySelector(".shit");
    let big = document.querySelector(".big");
    let img = document.querySelector("img");
    let bigBox = document.querySelector(".bigBox")
    big.addEventListener("mousemove", function(e) {
        mask.style.display = "block";
        bigBox.style.display = "block";
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        // console.log(x, y);
        //盒子高度300的一半是150
        mask.style.left = x - mask.offsetwidth / 2 + 'px';
        mask.style.top = y - mask.offsetHeight / 2 + 'px';

        if (x < 0) //限制左边
        {
            x = 0
        } else if (x > 150) //限制右边
        {
            // 250 - 100
            x = 150
        }

        if (y < 0) {
            y = 0
        } else if (y > 150) {
            // 250 - 100
            y = 150
        }

    })
    big.onmouseout = function() {
        //隐藏元素
        mask.style.display = 'none';
        bigBox.style.display = 'none';

    }
</script>

</html>